define(function(require,exports,module){
	
	require('common');
	//检测类
	var Auth = require('qd-auth-1.0');

	
			//-S 添加/修改地址
			$(".addressForm").validate({
				rules:{
					phoneNumber:{
						 required:function(){
						 	if($('.areaCode').val() == '')
						 	{
						 		return true;
						 	}
						 	else
						 	{
						 		return false;
						 	}
						 },
						 isMobile:true
					},
					areaCode:{
						 required:function(){
						 	if($('.phoneNumber').val() == '')
						 	{
						 		return true;
						 	}
						 	else
						 	{
						 		return false;
						 	}
						 }
					},
					addressName:{
						required:true,
						rangelength:[2,25],
						chnName:true
					},
					eCode:{
						isZipCode:true
					},
					detailAddress:{
						required:true,
						rangelength:[5,120]
					}
				},
				messages:{
					addressName:{
						required:"请输入您的姓名",
						rangelength:"收货人姓名应为2-25个字符，一个汉字为一个字符",
						chnName:"真实姓名仅为中文"
					},
					phoneNumber:{
						required:"请输入手机号码",
						isMobile:"请正确输入您的手机号码"
					},
					areaCode:{
						required:"请输入电话号码",
						isMobile:"请正确输入您的电话号码"
					},
					eCode:{
						isZipCode:"请正确输入邮政编码"
					},
					detailAddress: {
						required: "请输入详细地址",
						rangelength:"5-120个字符，一个汉字为一个字符"
					  }
				},
				errorElement:'em',
				errorPlacement:function(error,element){
					element.parents("li").find("span").html(error);
			    },
			    success:function(label){
			    },  
			    submitHandler:function(form){
	    			require.async('jquery.form',function(){
	    				$(".addressForm").ajaxSubmit({
	    					dataType:'json',
	    					success:function(data){

								//单点登录检测
								if(Auth.checkLogin(arguments[0]) == false) {return ;}

								require.async(['/plugins/layer-v1.8.4/skin/layer.css','/plugins/layer-v1.8.4/layer.min'],function(){
		    						if(data.result){
		    							 $.layer({
			    							    type: 1,
			    							    closeBtn: [0,true],
    				                		    title: "&nbsp;",
			    							    area: ['384px', '186px'],
			    							    border: [1, 1, '#cecfd0'],
			    							    time:3,
			    							    page: {
			    							        html: '<div class="tipsWrap w384"><div class="tipsTxt"><i class="iconfont okIco">&#xe63d;</i><span>实名认证申请成功，等待系统审核</span></div><div class="tipsMsg">3秒后窗口自动关闭</div></div>'
			    							    },
			    							    close: function(index){
			    							    	window.location.reload();
			    							    },
			    							    end: function(){
			    							    	window.location.reload();
			    							    },
			    							    success: function(layero){
			    							    	var time =3; 
			    							    	function closeTime () {
			    										time--;
			    										$(".tipsMsg").html(time+"秒后窗口自动关闭");
				    								}
			    							    	setInterval(function() {
		    											closeTime();
		    										}, 1000);
			    							    }
			    							    
			    						});
			    					}else{
			    						$.layer({
										    type: 1,
										    closeBtn: [0,true],
							                		    title: "&nbsp;",
										    area: ['384px', '186px'],
										    border: [1, 1, '#cecfd0'],
										    page: {
										        html: '<div class="tipsWrap w384"><div class="tipsTxt"><i class="iconfont errIco">&#xe63e;</i><span>'+111+'</span></div><div class="tipsBtnBar"><a href="javascript:;" class="okBtn failBtn">确定</a></div></div>'
										    },
										    close: function(index){
										    	window.location.reload();
										    }
										});
										$(".failBtn").click(function(){
											window.location.reload();
										});
			    					}
		    					})
	    					}
	    				});
	    			});
		        } 
			})	
	//-E 添加/修改地址

	//加载城市选择插件
	require.async('jquery-citySelect/jquery.cityselect',function(){
		$("#city").citySelect({
			url:"../../../themes/theme_default/media/js/jquery-citySelect/city.json",
			prov:"浙江", //省份 
			city:"杭州", //城市 
			dist:"西湖区", //区县 
		    required:true,
		    nodata:"none" //当子集无数据时，隐藏select 
		});
	})

		//通用显示函数
		function ajaxUrl(){
		  $.ajax({
		    type:'get',
		    cache:false,
		    url:'xxxxxx.html?randomTime=' + (new Date()).getTime(),
		    dataType:'json',
		    success:function(json){
	            $(".addressTable").html(Handlebars.compile(require('../../tpl/member/addressTable.tpl'))(json));
		        //分页插件
		        if(json.data.list.length > 0)
		        {
		          require.async(['/plugins/pager/pager.css','/plugins/pager/pager'],function(){
		            kkpager.generPageHtml({
		              pno : json.data.page.currentPage,//当前页码
		              total : json.data.page.pages,//总页码
		              totalRecords : json.data.page.total,//总数据条数
		              isShowFirstPageBtn  : false, 
		              isShowLastPageBtn : false, 
		              isShowTotalPage   : false, 
		              isShowTotalRecords  : false, 
		              isGoPage      : false,
		              lang:{
		                prePageText       : '<',
		                nextPageText      : '>'
		              },
		              mode:'click',//click模式匹配getHref 和 click
		              click:function(n,total,totalRecords){
		                        $.ajax({
		                          type:"get",
		                          cache:false,
		                          url:'xxxxxxx.html?'+'page=' + n + '&randomTime=' + (new Date()).getTime(),
		                          dataType:"json",//这个必不可少，如果缺少，导致传回来的不是json格式
		                          success:function(json){
									  //单点登录检测
									  if(Auth.checkLogin(arguments[0]) == false) {return ;}
									  if(json.result==false&&json.msg!=undefined){
											require.async(['/plugins/layer-v1.8.4/skin/layer.css','/plugins/layer-v1.8.4/layer.min'],function(){
												$.layer({
													type: 1,
										            closeBtn:true,
										            title: "&nbsp;",
										            area: ['384px', '186px'],
										            border: [1, 1, '#cecfd0'],
										            page: {
												        html: '<div class="tipsWrap w384"><div class="tipsTxt"><i class="iconfont errIco">&#xe63e;</i><span>'+json.msg+'</span></div><div class="tipsBtnBar"><a href="javascript:;" class="okBtn failBtn">确定</a></div></div>'
												    }
										        }); 
											});	
											return;
										}
		                           	  $(".addressTable").html(Handlebars.compile(require('../../tpl/member/addressTable.tpl'))(json));
		                          }
		                        });
		                this.selectPage(n); //处理完后可以手动条用selectPage进行页码选中切换
		              }
		            });
		          });
		        }else{
		          $("#kkpager").html('暂无数据');
		        }
		      }
		  })  
		}

		//删除地址
		$(".deletRecord").on("click",function(){
			var re_url = $('.lineId').attr("data-val");
			require.async(['/plugins/layer-v1.8.4/skin/layer.css','/plugins/layer-v1.8.4/layer.min'],function(){
			//构造确认框DOM
			$.layer({
			    type: 1,
			    closeBtn: [0,true],
    		    title: "&nbsp;",
			    area: ['450px', '190px'],
			    border: [1, 1, '#cecfd0'],
			    page: {
			        html: '<div class="tipsWrap w450"><div class="tipsTxt"><i class="iconfont tipIco">&#xe63c;</i><span>确定删除这条地址？</span></div><div class="tipsBtnBar"><a href="javascript:;" class="okBtn">确认</a><a href="javascript:;" class="cancleBtn">取消</a></div></div>'
			    }
			});	
			//确认操作
			$(".okBtn").click(function(){
				$.ajax({
					url:re_url,
					type:"post",
					success: function(data){

							//单点登录检测
						if(Auth.checkLogin(arguments[0]) == false) {return ;}

							layer.closeAll();
							$.layer({
							    type: 1,
							    closeBtn: [0,true],
	                		    title: "&nbsp;",
							    area: ['384px', '186px'],
							    border: [1, 1, '#cecfd0'],
							    time:3,
							    page: {
							        html: '<div class="tipsWrap w384"><div class="tipsTxt"><i class="iconfont okIco">&#xe63d;</i><span>成功删除本条地址</span></div><div class="tipsMsg">3秒后窗口自动关闭</div></div>'
							    },
							    close: function(index){
							    	window.location.reload();
							    },
							    end: function(){
							    	window.location.reload();
							    },
							    success: function(layero){
							    	var time =3; 
							    	function closeTime () {
										time--;
										$(".tipsMsg").html(time+"秒后窗口自动关闭");
    								}
							    	setInterval(function(){
										closeTime();
									}, 1000);
							    }
							});					
					}			
				});			
			});
			//删除操作
			$(".cancleBtn").click(function(){
				layer.closeAll();
			});
		}); 
	});
});
